package com.info3k.client.commands.user;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.info3k.client.Info3K;
import com.info3k.client.components.GlassDialogBox;
import com.info3k.client.components.InfoWindow;
import com.info3k.client.components.recaptcha.RecaptchaWidget;
import com.info3k.client.components.validators.TextBoxValidator;

public class RecoverPasswordCommand extends GlassDialogBox implements Command {
		
	private Info3K info3k;
	
	private Button recoverButton = new Button(Info3K.msgs.recoverPassword());
	
	private TextBoxValidator username = new TextBoxValidator();
	private RecaptchaWidget recaptcha = new RecaptchaWidget("6Ld_8AkAAAAAACa_FB4gLUXuae28YVOHTucoBFry");
	
	public RecoverPasswordCommand(Info3K info){
		this.info3k = info;
		final Label lblUsername = new Label(Info3K.msgs.username() + ": ");
		setText(Info3K.msgs.recoverPassword());
		content.add(lblUsername);
		content.add(username);
		content.add(recaptcha);
		
		username.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				username.setValid(username.getText().length()>0);
			}
		});
		
		footer.add(recoverButton);
		
		recoverButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(username.isValid()){
					if(recaptcha.getResponse()!=null && !(recaptcha.getResponse().length()>0)) {
						InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.invalidCaptcha());
						recaptcha.reload();
						return;
					}
					
					Info3K.loader.setVisible(true);
				
					Info3K.infoService.recoverPassword(username.getText(), recaptcha.getResponse(), recaptcha.getChallenge(), new AsyncCallback<Boolean>(){
						public void onFailure(Throwable caught) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
						}
	
						public void onSuccess(Boolean result) {
							Info3K.loader.setVisible(false);
							if(result==null){
								InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.usernameNotExist());
								recaptcha.reload();
							} else if(result){
								hide();
								InfoWindow.showMessage(Info3K.msgs.ok(), Info3K.msgs.recoverPasswordSendEmail());
							} else {
								InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.invalidCaptcha());
								recaptcha.reload();
							}
						}
						
					});
				} else InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.pleaseFillAllFields());
			}
		});
		
		recoverButton.getElement().getStyle().setProperty("margin", "0 10px 0 0");
		footer.add(closeButton);
	}
	
	public void execute() {
		center();
	}
}
